-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====
 
Location = {};
 
Location.__index = Location;
 
function Location:new()
    
    local self = {};
    setmetatable(self, Location);
     
    self.key = "Unknown";
    self.continent = "Unknown";
    self.zone = "Unknown";
    self.subZone = "Unknown";
    self.xMapCoord = 0.5;
    self.yMapCoord = 0.5;
    self.distanceToMapCoord = 0.0005;
    self.info = "Unknown";
    
    return self;
end

--[ Method to set key to location object ]-- 
function Location:setKey( newKey )
     self.key = newKey;
end

--[ Method to get key from location object ]--
function Location:getKey()
     return self.key;
end

--[ Method to set continent to location object ]-- 
function Location:setContinent( newContinent )
     self.continent = newContinent;
end

--[ Method to get continent from location object ]--
function Location:getContinent()
     return self.continent;
end

--[ Method to set zone to location object ]-- 
function Location:setZone( newZone )
     self.zone = newZone;
end

--[ Method to get zone from location object ]--
function Location:getZone()
     return self.zone;
end

--[ Method to set subZone to location object ]-- 
function Location:setSubZone( newSubZone )
     self.subZone = newSubZone;
end

--[ Method to get subZone from location object ]--
function Location:getSubZone()
     return self.subZone;
end

--[ Method to set xMapCoord to location object ]-- 
function Location:setXMapCoord( newXMapCoord )
     self.xMapCoord = newXMapCoord;
end

--[ Method to get xMapCoord from location object ]--
function Location:getXMapCoord()
     return self.xMapCoord;
end

--[ Method to set yMapCoord to location object ]-- 
function Location:setYMapCoord( newYMapCoord )
     self.yMapCoord = newYMapCoord;
end

--[ Method to get yMapCoord from location object ]--
function Location:getYMapCoord()
     return self.yMapCoord;
end

--[ Method to set distanceToMapCoord to location object ]-- 
function Location:setDistanceToMapCoord( newDistanceToMapCoord )
     self.distanceToMapCoord = newDistanceToMapCoord;
end

--[ Method to get distanceToMapCoord from location object ]--
function Location:getDistanceToMapCoord()
     return self.distanceToMapCoord;
end

--[ Method to set info to location object ]-- 
function Location:setInfo( newInfo )
     self.info = newInfo;
end

--[ Method to get info from location object ]--
function Location:getInfo()
     return self.info;
end

--[ Method to floor map coord ]--
function Location:MapFloor( val )
    local newVal = math.floor( val * 1000000 ) / 1000000;
    return newVal;
end

--[ Method to test if the player current position is included in this location ]--
function Location:playerIsPresent()

    -- Get location information of player
    local continents = { GetMapContinents() };
    SetMapToCurrentZone();
    local currentValue = GetCurrentMapContinent();
    local continent = "Instance"
    if ( currentValue > 0 ) 
    then
        continent = continents[ currentValue ];
    end
    local zone = GetRealZoneText();
    local subZone = GetMinimapZoneText();
    local posX, posY = GetPlayerMapPosition("player");
    

    if (    self.continent == continent and
            self.zone == zone and
            self.subZone == subZone )
    then
    
        local refX = self.xMapCoord;
        local refY = self.yMapCoord;
        
        local computeDistance = self:MapFloor( math.sqrt ( ( posX - refX ) * ( posX - refX ) + ( posY - refY ) * ( posY - refY ) ) );
        
        SBMessageManager:debugMessage("computeDistance = "..computeDistance);
         SBMessageManager:debugMessage("distanceToMapCoord = "..self.distanceToMapCoord);
        if ( computeDistance <= self.distanceToMapCoord )
        then
           return true;
        else
            return false;
        end
    
    else
       return false;
    end
end

function Location:capturePlayerZone()

    SBMessageManager:debugMessage("function Location:capturePlayerZone()");
    
    -- Get location information of player
    local continents = { GetMapContinents() };
    SetMapToCurrentZone();
    local currentValue = GetCurrentMapContinent();
    local continent = "Instance"
    if ( currentValue > 0 ) 
    then
        continent = continents[ currentValue ];
    end
    local zone = GetRealZoneText();
    local subZone = GetMinimapZoneText();
    local posX, posY = GetPlayerMapPosition("player");
    posX = Location:MapFloor( posX );
    posY = Location:MapFloor( posY );
    
    -- Set values
    self.continent = continent;
    self.zone = zone;
    self.subZone = subZone;
    self.xMapCoord = posX;
    self.yMapCoord = posY;
    
    
    -- Set default key 
      self.key = "Unknown";
    if ( self.key == "Unknown" )
    then
        local x, y;
        x = math.floor( self.xMapCoord * 1000 );
        y = math.floor( self.yMapCoord * 1000 );
        self.key = self.subZone.."_"..x.."_"..y; 
    end
    
    -- Set default desc
    if ( self.info == "Unknown" )
    then
        self.info = "Sorry, no description for "..self.key.." ."; 
    end
end

--[ Method to configure a location from a data string ]--
function Location:import( data )
    success, self.key, self.continent, self.zone, self.subZone, self.xMapCoord, self.yMapCoord, self.distanceToMapCoord, self.info = StoryBoard:Deserialize( data );
end

--[ Method to export a location in a data string format ]--
function Location:export()
    data = StoryBoard:Serialize( self.key, self.continent, self.zone, self.subZone, self.xMapCoord, self.yMapCoord, self.distanceToMapCoord, self.info );
    return data;
end


function Location:exportToTable()
    
    local dataTable = {} ;
    
    table.insert( dataTable , self.key );
    table.insert( dataTable , self.continent );
    table.insert( dataTable , self.zone );
    table.insert( dataTable , self.subZone );
    table.insert( dataTable , self.xMapCoord );
    table.insert( dataTable , self.yMapCoord );
    table.insert( dataTable , self.distanceToMapCoord );
    table.insert( dataTable , self.info );

    return dataTable;
     
end



function Location:importFromTable( dataTable )
    
    self.key = dataTable[1];
    self.continent = dataTable[2];
    self.zone = dataTable[3];
    self.subZone = dataTable[4];
    self.xMapCoord = dataTable[5];
    self.yMapCoord = dataTable[6];
    self.distanceToMapCoord = dataTable[7];
    self.info = dataTable[8];
   

end